<?php
/**
* 
* ShopWindow Toolset
* 
* Copyright (C) 2008 Digital Window Ltd.
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*
*/


// used further down and for Smarty
$sCurrentPage = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];


$iProductsTotalCount 	= $oShopCore->oApiProductList->iTotalCount;
$iProductsLimit		 	= $oShopCore->oApiProductList->iLimit;
$iProductsOffset		= $oShopCore->oApiProductList->iOffset;


// limit to a 1000 products
$iRealProductsTotalCount 	= $iProductsTotalCount;
$iProductsTotalCount 		= $iProductsTotalCount > 1000 ? 1000 : $iProductsTotalCount;


# Navigation
if ( $iProductsTotalCount > $iProductsLimit )
{
	// remove previous limit
	$sPageQuery= preg_replace('/&?iListOffset=\d*/', '', $_SERVER['QUERY_STRING']);
	// remove image submit co-ordinates crap
	$sPageQuery= preg_replace('/&?search\.[x|y]=\d*/', '', $sPageQuery);

	// see if there were params left in query
	if (strlen($sPageQuery)>0) {
		$sPageUrl= $sCurrentPage.'?'.$sPageQuery.'&iListOffset=';
	}
	else {
		$sPageUrl= $sCurrentPage.'?iListOffset=';
	}


	# Draw the previous href
	if ( $iProductsOffset>0 ) {
		$iPrevious = ( ($iProductsOffset - $iProductsLimit) < 0 ? '0' : ($iProductsOffset - $iProductsLimit) );

		$sPreviousLink= $sPageUrl.$iPrevious;
	}

	# Draw the next href
	if ( ($iProductsOffset + $iProductsLimit) < $iProductsTotalCount ) {
		$iNext = ( $iProductsLimit+$iProductsOffset );

		$sNextLink= $sPageUrl.$iNext;
	}


	# Pagenator
	$aPagenator= array();
	$iOffset= 0;

	// set starting point
	$i= !empty($iProductsOffset) ? ($iProductsOffset/$iProductsLimit) : 1;
	// keep selected page in the middle
	$i= ($i-3)<0 ? 0 : $i-3;

	$iLast= $i+10; // 10 is the number of visible page links

	while ( ($i < $iLast) && ($iOffset < $iProductsTotalCount) ) {
		$iOffset= ($i-1)*$iProductsLimit;
		$aPagenator[$i]= $sPageUrl.$iOffset;
		$i++;
	}

	// there's always one extra at the end so pop it
	array_pop($aPagenator);
	// and sometimes at the beginning
	unset($aPagenator[0]);

	// overwrite the first page link
	$aPagenator[1]= $sPageUrl.'0';

	// add the last page link
	$iLastPage= @floor($iProductsTotalCount / $iProductsLimit);
	// if there's no remainder, that means the previous page will contain tha last product
	// therefore remove the last page, which would have been empty anyway
	$iLastPage= @($iProductsTotalCount % $iProductsLimit)==0 ?  $iLastPage-1 : $iLastPage;

	$aPagenator[$iLastPage+1]= $sPageUrl.($iLastPage * $iProductsLimit);

	// sort ASC
	ksort($aPagenator);
}


# Construct values for the limit select box
$aSelectLimits = array('10'=>10, '20'=>20, '30'=>30, '40'=>40, '50'=>50);


# Construct values for the sort select box
$aSelectSorts = array();

// add relevancy to the box if we are on the search page
if ( strlen($oShopCore->sQuery) > 0 ) {
	$aSelectSorts['relevancy'] = 'Relevancy';
}

$aSelectSorts['popularity'] = 'Popularity';
$aSelectSorts['lo'] = 'Price: Low-High';
$aSelectSorts['hi'] = 'Price: High-Low';
$aSelectSorts['az'] = 'Name: A-Z';
$aSelectSorts['za'] = 'Name: Z-A';



// so we know which page to highlight
$iCurrentPage= ($iProductsOffset/$iProductsLimit)+1;



# SMARTY ASSIGN
$oSmarty->assign('PRODUCTLIST_PAGE', P_PRODUCTLIST);

$oSmarty->assign('sQuery', $oProductDisplayListParams->sQuery);
$oSmarty->assign('iCategoryId', $oProductDisplayListParams->iCategoryId);
$oSmarty->assign('iMerchantId', $oProductDisplayListParams->iMerchantId);

$oSmarty->assign('sRb', $oShopCore->createRefineByQuery($oShopCore->aRefineByActiveVals));

$oSmarty->assign('aSelectLimits', $aSelectLimits);
$oSmarty->assign('aSelectSorts', $aSelectSorts);

$oSmarty->assign('iCurrentPage', $iCurrentPage);
$oSmarty->assign('iListOffset', $oProductDisplayListParams->iOffset);
$oSmarty->assign('iListLimit', $oProductDisplayListParams->iLimit);
$oSmarty->assign('sListSort', $oProductDisplayListParams->sSort);

$oSmarty->assign('sPreviousLink', $sPreviousLink);
$oSmarty->assign('sNextLink', $sNextLink);
$oSmarty->assign('aPagenator', $aPagenator);

$oSmarty->assign('iTotalProducts', $iRealProductsTotalCount);
$oSmarty->assign('iPageProducts', $iProductsOffset + $oShopCore->oApiProductList->iReturnCount);



?>